<!DOCTYPE html>
<html>
<head>
  <title>ChatGPT 3.5-turbo</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f5f5f5;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }

    .container {
      max-width: 1000px;
      width: 100%;
      height: 80vh;
      padding: 20px;
      background-color: #fff;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      display: flex;
      flex-direction: column;
    }

    h1 {
      text-align: center;
      margin-bottom: 20px;
    }

    .chat-log {
      flex: 1;
      overflow-y: scroll;
      padding-right: 10px;
      border-right: 1px solid #ccc;
      margin-bottom: 20px;
    }

    .user-message {
      background-color: #f5f5f5;
      color: #333;
      border-radius: 5px;
      padding: 10px;
      margin-bottom: 10px;
      align-self: flex-end;
    }

    .ai-message {
      background-color: #007bff;
      color: #fff;
      border-radius: 5px;
      padding: 10px;
      margin-bottom: 10px;
      align-self: flex-start;
    }

    .input-container {
      display: flex;
      align-items: center;
    }

    .input {
      flex: 1;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
    }

    .send-button {
      margin-left: 10px;
      padding: 10px 20px;
      background-color: #007bff;
      color: #fff;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }

    .loading {
      text-align: center;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>ChatGPT 3.5-turbo</h1>
    <div class="chat-log" id="chatLog"></div>
    <div class="input-container">
      <input class="input" id="inputMessage" placeholder="请输入消息"></input>
      <button class="send-button" onclick="sendMessage()">发送</button>
    </div>
    <div class="loading" id="loading" style="display: none;">正在加载...</div>
  </div>

  <script>
    const chatLog = document.getElementById('chatLog');
    const inputMessage = document.getElementById('inputMessage');
    const loading = document.getElementById('loading');

    function sendMessage() {
      const message = inputMessage.value.trim();

      if (message === '') {
        return;
      }

      addMessageToChatLog(message, 'user-message');
      inputMessage.value = '';

      // 显示加载指示器
      loading.style.display = 'block';

      // 发送消息到服务器（ChatGPT）进行处理
      sendRequestToChatGPT(message);
    }

    function sendRequestToChatGPT(message) {
      // 构建请求对象
      const request = {
        "model": "gpt-3.5-turbo",
        "messages": [{
          "role": "user",
          "content": message
        }]
      };

      // 发送请求到服务器（这里使用示例的URL，根据实际情况修改）
      fetch('https://api.chatanywhere.com.cn/v1/chat/completions', {
        method: 'POST',
        headers: {
        'Authorization': 'sk-nejgbSZirjUTB61dynxVk6NHdFgSkDGtcCHsKFYWBNDx5WHg',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(request)
      })
      .then(response => response.json())
      .then(data => {
        console.log(data);
        const reply = data.choices[0].message.content;

        // 将服务器返回的回复添加到聊天记录中
        addMessageToChatLog(reply, 'ai-message');
      })
      .catch(error => {
        console.error('请求ChatGPT时出错:', error);
        alert('请求ChatGPT时出错，请稍后再试。');
      })
      .finally(() => {
        // 隐藏加载指示器
        loading.style.display = 'none';
      });
    }

    function addMessageToChatLog(message, className) {
      const messageElement = document.createElement('div');
      messageElement.textContent = message;
      messageElement.classList.add(className);
      chatLog.appendChild(messageElement);
      chatLog.scrollTop = chatLog.scrollHeight;
    }
  </script>
</body>
</html>
